#ifndef __Matutils__
#define __Matutils__
#pragma once
#include <io.h>
#include <stdlib.h>

#include <iostream>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/opencv.hpp>
#include <string>
#include <vector>
using namespace std;
using namespace cv;

// 对图像填充黑边
inline Mat pad_mat(Mat img, int pad = 10)
{
    Mat imgWindow = Mat::zeros(img.cols + 2 * pad, img.rows + 2 * pad, img.type());
    img.copyTo(imgWindow(Rect({pad, pad}, img.size())));
    return imgWindow;
}
// 移除图像填充的黑边
inline Mat rm_pad(Mat img, int pad = 10)
{
    Size size = {img.cols - 2 * pad, img.rows - 2 * pad};
    return img(Rect({pad, pad}, size));
}
// 图像拼接
inline void multipleImage(vector<Mat> imgVector, Mat& dst, int imgCols, int MAX_PIXEL = 300)
{
    // 两列图像间的空白区域
    int pad = 10;
    int imgNum = imgVector.size();
    // 选择图片最大的一边 将最大的边按比例变为300像素
    Size imgOriSize = imgVector[0].size();
    int imgMaxPixel = max(imgOriSize.height, imgOriSize.width);
    // 获取最大像素变为MAX_PIXEL的比例因子
    double prop =
        imgMaxPixel < MAX_PIXEL ? (double)imgMaxPixel / MAX_PIXEL : MAX_PIXEL / (double)imgMaxPixel;
    Size imgStdSize(imgOriSize.width * prop, imgOriSize.height * prop);  // 窗口显示的标准图像的Size

    Mat imgStd;              // 标准图片
    Point2i location(0, 0);  // 坐标点(从0,0开始)
    // Mat imgWindow(imgStdSize.height * ((imgNum - 1) / imgCols + 1), imgStdSize.width * imgCols+
    // pad * imgCols-pad, imgVector[0].type());
    int imgRows = (imgNum - 1) / imgCols + 1;
    Mat imgWindow =
        Mat::zeros(imgStdSize.height * imgRows + pad * imgRows - pad,
                   imgStdSize.width * imgCols + pad * imgCols - pad, imgVector[0].type());
    for (int i = 0; i < imgNum; i++)
    {
        location.x = (i % imgCols) * (imgStdSize.width + pad);
        location.y = (i / imgCols) * imgStdSize.height;
        resize(imgVector[i], imgStd, imgStdSize, prop, prop, INTER_LINEAR);  // 设置为标准大小

        // 将imgStd复制到imgWindow的指定区域中
        imgStd.copyTo(imgWindow(Rect(location, imgStdSize)));
    }
    dst = imgWindow;
}
// 多图显示
inline void imshows(string title, vector<Mat> imgVector, int imgCols = -1)
{
    Mat dst;
    if (imgCols == -1)
    {
        imgCols = imgVector.size();
    }
    multipleImage(imgVector, dst, imgCols);
    namedWindow(title);
    imshow(title, dst);
    imwrite(title + ".png", dst);
}
// 删除小面积的连通域
inline Mat deleteMinWhiteArea(Mat src, int min_area)
{
    Mat labels, stats, centroids, img_color, grayImg;
    // 1、连通域信息统计
    int nccomps = connectedComponentsWithStats(src,  // 二值图像
                                               labels, stats, centroids);

    // 2、连通域状态区分
    // 为每一个连通域初始化颜色表
    vector<Vec3b> colors(nccomps);
    colors[0] = Vec3b(0, 0, 0);  // background pixels remain black.
    for (int i = 1; i < nccomps; i++)
    {
        colors[i] = Vec3b(rand() % 256, rand() % 256, rand() % 256);
        // 面积阈值筛选
        if ((stats.at<int>(i, CC_STAT_AREA) < min_area))
        {
            // 如果连通域面积不合格则置黑
            colors[i] = Vec3b(0, 0, 0);
        }
    }
    // 3、连通域删除
    // 按照label值，对不同的连通域进行着色
    img_color = Mat::zeros(src.size(), CV_8UC3);
    for (int y = 0; y < img_color.rows; y++)
    {
        int* labels_p = labels.ptr<int>(y);            // 使用行指针，加速运算
        Vec3b* img_color_p = img_color.ptr<Vec3b>(y);  // 使用行指针，加速运算
        for (int x = 0; x < img_color.cols; x++)
        {
            int label = labels_p[x];  // 取出label值
            CV_Assert(0 <= label && label <= nccomps);
            img_color_p[x] = colors[label];  // 设置颜色
        }
    }
    // return img_color;
    // 如果是需要二值结果则将img_color进行二值化
    cvtColor(img_color, grayImg, COLOR_BGR2GRAY);
    threshold(grayImg, grayImg, 1, 255, THRESH_BINARY);
    return grayImg;
}
// 删除图形中小面积的黑色孔洞
inline Mat deleteMinBlackArea(Mat src, int min_area)
{
    Mat inv = 255 - src;  // 颜色取反
    Mat res = deleteMinWhiteArea(inv, min_area);
    return 255 - res;  // 颜色取反
}
// 找图中topk个连通域
inline bool mypairsort(pair<int, int> i, pair<int, int> j) { return (i.second > j.second); }
inline Mat findTopKArea(Mat srcImage, int topk)
{
    Mat temp;
    Mat labels;
    srcImage.copyTo(temp);

    // 1. 标记连通域
    int n_comps = connectedComponents(temp, labels, 4, CV_16U);
    vector<pair<int, int>> histogram_of_labels;
    for (int i = 0; i < n_comps; i++)  // 初始化labels的个数为0
    {
        histogram_of_labels.push_back({i, 0});
    }

    int rows = labels.rows;
    int cols = labels.cols;
    for (int row = 0; row < rows; row++)  // 计算每个labels的个数--即连通域的面积
    {
        for (int col = 0; col < cols; col++)
        {
            histogram_of_labels.at(labels.at<unsigned short>(row, col)).second += 1;
        }
    }
    // histogram_of_labels.at(0).second = 0; //将背景的labels个数设置为0

    // 2.对连通域进行排序
    std::sort(histogram_of_labels.begin(), histogram_of_labels.end(), mypairsort);
    // 3. 取前k个连通域的labels id
    vector<int> select_labels;
    for (int i = 0; i < topk; i++)
    {
        if (histogram_of_labels[i].first == 0)
        {
            topk += 1;
            // 如果碰到背景，则跳过，且topk+1
        }
        else
        {
            select_labels.push_back(histogram_of_labels[i].first);
        }
    }

    // 3. 将label id在select_labels的连通域标记为255，并将其他连通域置0
    for (int row = 0; row < rows; row++)
    {
        for (int col = 0; col < cols; col++)
        {
            int now_label_id = labels.at<unsigned short>(row, col);
            if (std::count(select_labels.begin(), select_labels.end(), now_label_id))
            {
                labels.at<unsigned short>(row, col) = 255;
            }
            else
            {
                labels.at<unsigned short>(row, col) = 0;
            }
        }
    }

    // 4. 将图像更改为CV_8U格式
    labels.convertTo(labels, CV_8U);
    return labels;
}
// 获取脊线
inline Mat get_ridge_line(Mat dst, int ksize = 3)
{
    Mat skeleton, result, open_dst;
    Mat kernel = getStructuringElement(MORPH_CROSS, Size(ksize, ksize));
    skeleton = Mat::zeros(dst.rows, dst.cols, dst.type());
    while (true)
    {
        if (sum(dst)[0] == 0)
        {
            break;
        }
        morphologyEx(dst, dst, MORPH_ERODE, kernel);  // 消除毛刺，删除部分连通域
        morphologyEx(dst, open_dst, MORPH_OPEN, kernel);
        result = dst - open_dst;
        skeleton = skeleton + result;
    }
    return skeleton;
}

Mat sobel_mat(Mat srcImage)
{
    Mat grad_x, grad_y;
    Mat abs_grad_x, abs_grad_y;
    //    计算x方向的梯度
    Sobel(srcImage, grad_x, CV_16S, 1, 0, 3, 1, 0, BORDER_DEFAULT);
    convertScaleAbs(grad_x, abs_grad_x);

    //    计算y方向的梯度
    Sobel(srcImage, grad_y, CV_16S, 0, 1, 3, 1, 0, BORDER_DEFAULT);
    convertScaleAbs(grad_y, abs_grad_y);

    //    合并梯度
    Mat dstImage;
    addWeighted(abs_grad_x, 0.5, abs_grad_y, 0.5, 0, dstImage);
    return dstImage;
}
Mat line2red(Mat line)
{
    vector<cv::Mat> mv;
    Mat red_line;
    Mat zeros = Mat::zeros(line.size(), CV_8UC1);
    // mv.push_back(zeros);
    // mv.push_back(zeros);
    mv.push_back(line);
    mv.push_back(line);
    mv.push_back(line);
    // 将vector内的多个mat合并为一个多通道mat
    cv::merge(mv, red_line);
    return red_line;
}
Mat get_by(Mat roi)
{
    Mat roi_left, roi_erode, roi_by;
    Mat element = getStructuringElement(MORPH_RECT, Size(7, 7));
    roi_left = pad_mat(roi);
    morphologyEx(roi_left, roi_erode, MORPH_ERODE, element);
    roi_by = roi_left - roi_erode;
    roi_by = rm_pad(roi_by);
    // cvtColor(roi_by, roi_by, color);
    return line2red(roi_by);
}

#endif
